package com.invengo.testrfid.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.invengo.testrfid.R;
import com.invengo.testrfid.app.utils.SoundPlayUtils;
import com.invengo.testrfid.di.component.DaggerUpdateCageComponent;
import com.invengo.testrfid.di.module.UpdateCageModule;
import com.invengo.testrfid.mvp.contract.UpdateCageContract;
import com.invengo.testrfid.mvp.presenter.UpdateCagePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CageManagerActivity extends BaseActivity<UpdateCagePresenter> implements
        UpdateCageContract.View {

    @BindView(R.id.tv_tid)
    TextView tv_tid;

    @BindView(R.id.tv_tid_second)
    TextView tv_tid_second;

    @BindView(R.id.et_c_no)
    EditText et_c_no;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUpdateCageComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .updateCageModule(new UpdateCageModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.connectReader();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.disConnectReader();
    }




    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_update_cage; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void clearCageNo() {

        et_c_no.setText("");
        tv_tid.setText("Tid1");
        tv_tid_second.setText("Tid2");
    }

    @Override
    public void initTid(String tid,String epc) {

        SoundPlayUtils.getInstance().playSound(SoundPlayUtils.SOUNT_SUCCESS, 0);
        tv_tid.setText(tid);

        String trim = et_c_no.getText().toString().trim();

        if (epc.contains(trim) && trim.length()!= 0) {

            tv_tid.setTextColor(getResources().getColor(R.color.colorPrimary));
        }else {

            tv_tid.setTextColor(getResources().getColor(R.color.colorAccent));
        }
    }

    @Override
    public void initTidSecond(String tid,String epc) {

        SoundPlayUtils.getInstance().playSound(SoundPlayUtils.SOUNT_SUCCESS, 0);
        tv_tid_second.setText(tid);

        String trim = et_c_no.getText().toString().trim();
        showMessage(trim);

        if (epc.contains(trim) && trim.length()!= 0) {

            tv_tid_second.setTextColor(getResources().getColor(R.color.colorPrimary));
        }else {

            tv_tid_second.setTextColor(getResources().getColor(R.color.colorAccent));
        }
    }

    @Override
    public void initCageNo(String cageNo) {

        et_c_no.setText(cageNo);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyUp(keyCode, event);
        } else {
            mPresenter.stopReadTag();
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyDown(keyCode, event);
        } else {

            mPresenter.readTagTid(tv_tid, tv_tid_second);
            return true;
        }
    }

    public void updateCageNo(View view) {

        mPresenter.updateTheBindByTid(tv_tid.getText().toString(),
                tv_tid_second.getText().toString(), et_c_no.getText().toString());
    }

    public void updateCageTid(View view) {

        mPresenter.updateTheBindByCageNo(tv_tid.getText().toString(),
                tv_tid_second.getText().toString().trim(), et_c_no.getText().toString());
    }

    @OnClick(R.id.btn_clear_1)
    public void clearBtn1() {

        tv_tid.setText("Tid1");
    }

    @OnClick(R.id.btn_clear_2)
    public void clearBtn2() {

        tv_tid_second.setText("Tid2");
    }

    @OnClick(R.id.btn_search)
    public void searchBindByCageNo() {

        mPresenter.searchCageByNo(et_c_no.getText().toString().trim());
    }

    public void writeDataToTag(View view){

        mPresenter.writeData(et_c_no.getText().toString());
    }

}
